package com.leo.erp.web.depot;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.leo.erp.dto.DepotExecution;
import com.leo.erp.dto.UserBusinessExecution;
import com.leo.erp.entity.Depot;
import com.leo.erp.enums.DepotStateEnum;
import com.leo.erp.enums.UserBusinessStateEnum;
import com.leo.erp.service.DepotService;
import com.leo.erp.service.SystemConfigService;
import com.leo.erp.service.UserBusinessService;
import com.leo.erp.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/depot")
public class DepotManagementController {
	@Autowired
	private DepotService depotService;
	@Autowired
	private UserBusinessService userBusinessService;
	@Autowired
	private SystemConfigService systemConfigService;
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int currentPage = HttpServletRequestUtil.getInt(request, "currentPage");
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		String depotName = HttpServletRequestUtil.getString(request, "name");
		Integer type = HttpServletRequestUtil.getInt(request, "type");
		String remark = HttpServletRequestUtil.getString(request, "remark");
		DepotExecution de = depotService.select(depotName, type, remark, currentPage, pageSize);
		if(de.getState() == DepotStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("pages",de.getPages());//总页数
			modelMap.put("total", de.getTotal());//总数
			
			Map<String, Object> map = new HashMap<>();
			map.put("rows", de.getDepotPageList());
			map.put("total", de.getTotal());
			//modelMap.put("userList", ue.getUserPageList());
			modelMap.put("depotList", map);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询列表失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/updateDepotIsDefault")
	@ResponseBody
	public Map<String, Object> updateDepotIsDefault(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int isDefault = HttpServletRequestUtil.getInt(request, "isDefault");
		int depotId = HttpServletRequestUtil.getInt(request, "depotID");
		
		DepotExecution de = depotService.updateDepotIsDefault(isDefault, depotId);
		if(de.getState() == DepotStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "服务内部错误");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/batchDeleteDepotByIds")
	@ResponseBody
	public Map<String, Object> batchDeleteDepotByIds(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String ids = HttpServletRequestUtil.getString(request, "ids");
		String deleteType = HttpServletRequestUtil.getString(request, "deleteType");//1:正常删除,2:强制删除
		
		DepotExecution de = new DepotExecution();
		if("1".equals(deleteType)) {
			//正常删除
			de = depotService.batchDeleteDepotByIdsNormal(ids);
			if(de.getState() == 2) {
				modelMap.put("success", false);
				modelMap.put("code", 2);
				modelMap.put("msg", "希望强制删除吗?");
			}else if(de.getState() == DepotStateEnum.SUCCESS.getState() ) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除供应商信息失败");
			}
		}else if("2".equals(deleteType)) {
			//强制删除
			int effectedNum = depotService.batchDeleteDepotByIds(ids);
			if(effectedNum > 0) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除供应商信息失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "删除操作被拒绝，请联系管理员");
		}		
		
		return modelMap;
	}
	
	@RequestMapping(value = "/findDepotByUserId")
	@ResponseBody
	public JSONArray findDepotByUserId(HttpServletRequest request) {
		JSONArray arr = new JSONArray();
		//获取前台传过来的值
		String type = HttpServletRequestUtil.getString(request, "UBType");
		String keyId = HttpServletRequestUtil.getString(request, "UBKeyId");
		DepotExecution de = depotService.findUserDepot();
		if(de.getState() == DepotStateEnum.SUCCESS.getState()) {
			//开始拼接json数据
			boolean depotFlag = systemConfigService.getDepotFlag();
			for(Depot depot : de.getDepotList()) {
				JSONObject item = new JSONObject();
				//勾选判断1
                Boolean flag = false;
                UserBusinessExecution ue = userBusinessService.checkIsUserBusinessExist(type, keyId, "[" + depot.getDepotId() + "]");
                if(ue.getState() == UserBusinessStateEnum.SUCCESS.getState()) {
                	flag = true;
                }
                if (!depotFlag || flag) {
                    item.put("id", depot.getDepotId());
                    item.put("depotName", depot.getDepotName());
                    arr.add(item);
                }
			}
		}
		
		return arr;
	}
	
	@RequestMapping(value = "/getAllList")
	@ResponseBody
	public Map<String, Object> getAllList(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		DepotExecution de = depotService.getAllList();
		if(de.getState() == DepotStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("depotList", de.getDepotList());
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "获取数据失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/update")
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int depotId = HttpServletRequestUtil.getInt(request, "id");
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		
		DepotExecution de = depotService.updateDepot(beanJson, depotId, request);
		if(de.getState() == DepotStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "修改仓库信息失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		//调用方法
		DepotExecution de = depotService.insertDepot(beanJson, request);
		if(de.getState() == DepotStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/checkIsNameExist")
	@ResponseBody
	public Map<String, Object> checkIsNameExist(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int depotId = HttpServletRequestUtil.getInt(request, "id");
		String depotName = HttpServletRequestUtil.getString(request, "name");
				
		//调用方法
		DepotExecution de = depotService.checkIsNameExist(depotId, depotName);
		if(de.getState() == DepotStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);//存在
		}else {
			modelMap.put("success", false);
		}
		
		return modelMap;
	}
	
	/**
	 * 用户对应部门显示
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findUserDepot")
	@ResponseBody
	public JSONArray findUserDepot(HttpServletRequest request) {
		//获取前台传过来的值
		String type = HttpServletRequestUtil.getString(request, "UBType");
		String keyId = HttpServletRequestUtil.getString(request, "UBKeyId");
		JSONArray arr = new JSONArray();
		DepotExecution de = depotService.findUserDepot();
		//开始拼接json数据
        JSONObject outer = new JSONObject();
        outer.put("id", 1);
        outer.put("text", "仓库列表");
        outer.put("state", "open");
        //存放数据json数组
        JSONArray dataArray = new JSONArray();
		if(de.getState() == DepotStateEnum.SUCCESS.getState()) {
            for(Depot depot : de.getDepotList()) {
            	JSONObject item = new JSONObject();
                item.put("id", depot.getDepotId());
                item.put("text", depot.getDepotName());
                //勾选判断1
                Boolean flag = false;
                UserBusinessExecution ue = userBusinessService.checkIsUserBusinessExist(type, keyId, "[" + depot.getDepotId() + "]");
                if(ue.getState() == UserBusinessStateEnum.SUCCESS.getState()) {
                	flag = true;
                }
                if (flag == true) {
                    item.put("checked", true);
                }
                //结束
                dataArray.add(item);
            }
		}
		outer.put("children", dataArray);
        arr.add(outer);
		return arr;
	}
}
